import Vue from 'vue'
import Vuex from 'vuex'

import router from '../router'

import {login,logout} from "@/api/user"

import {  Message } from 'element-ui'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token:null,
    userinfo:null,
    // 面包屑
    breadcrumb: [],
    // 标签页
    tagView: [],
    // 生效标签页
    activeTagView: -1
  },
  getters: {
    state:state=>state,
    token:state=>state.token,
    userinfo:state=>state.userinfo,
    roles:state=>state.userinfo.roles,
    permissions:state=>{if(state.userinfo&&state.userinfo!=null){
      let list = []
				if (state.userinfo.roles != null && state.userinfo.roles.length > 0) {
					list = [...state.userinfo.roles]
				}
				if (state.userinfo.permissions != null && state.userinfo.permissions.length > 0) {
					list = [...list, ...state.userinfo.permissions]
				}
        return list
    }},
    breadcrumb: state => state.breadcrumb,
    tagView: state => state.tagView,
    activeTagView: state => { if (state.activeTagView == -1) { return { fullpath: "/home", name: 'home', title: '首页' } } else { return state.tagView[state.activeTagView] } }
  },
  mutations: {
    setState(state,storage){
      state.token=storage.token
      state.userinfo=storage.userinfo
      state.breadcrumb=storage.breadcrumb
      state.tagView=storage.tagView
      state.activeTagView=storage.activeTagView
    },
    setUserInfo(state,userinfo){
      state.userinfo=userinfo;
    },
    setToken(state,token){
      state.token=token;
    },
    setBreadCrumb(state, breadcrumb) {
      state.breadcrumb = breadcrumb
    },
    resetActiveTagView(state) {
      state.activeTagView = -1
      // state.tagView=[]
    },
    //解决mav点击页签不刷新
    fromNavRefreshTagView(state, tagView) {
      state.tagView.forEach((item, index) => {
        if (item.name == tagView.name) {
          state.activeTagView = index
        }
      })
    },
    // 选择标签页
    selectTagView(state, tagView) {
     
      if (tagView.name != "home") {
        const index = state.tagView.indexOf(tagView);
        state.activeTagView = index;
        router.push(tagView.name)
      } else {
        router.push("home")
      }

    },
    // 添加标签页
    addTagView(state, tagView) {
      state.tagView.push(tagView)
      state.activeTagView = state.tagView.length - 1
      // router.push(tagView.name)
    },
    //删除标签页
    deleteTagView(state, tagView) {
      const index = state.tagView.indexOf(tagView);
      state.tagView.splice(index, 1)

      if (index > state.activeTagView) { }
      if (index <= state.activeTagView) {
        state.activeTagView = state.activeTagView - 1;
        if (state.activeTagView != -1) {
          router.push(state.tagView[state.activeTagView].name)
        } else {
          router.push("home")
        }

      }
     

    },
    //删除右侧
    deleteRightTagView(state, tagView) {
      const index = state.tagView.indexOf(tagView);
      state.activeTagView = index
      
      state.tagView.splice(index + 1, state.tagView.length - 1 - index)
      router.push(state.tagView[state.activeTagView].name)
      


    },
    //删除左侧
    deleteLeftTagView(state, tagView) {

      const index = state.tagView.indexOf(tagView);
      state.activeTagView = 0
      for (var i = 0; i < index; i++) {
        
        state.tagView.shift()

      }

      router.push(state.tagView[state.activeTagView].name)
      state.activeTagView = 0

    },
    //删除全部
    deleteAllTagView(state) {
      
      state.tagView = []
      router.push("home")
      state.activeTagView = -1
    },
    //删除其他
    deleteOtherTagView(state, tagView) {
     
      state.tagView = [tagView]
      state.activeTagView = 0
      router.push(state.tagView[state.activeTagView].name)
    }
  },
  actions: {
    async getuserinfo(context,user){
     
        const res = await login(user)
        if(res.code==200){
          context.commit('setUserInfo',res.data)
          context.commit('setToken',res.data.token)
          router.push("home") 
        }
        return res; 
    },
    async exit(context){
      const res = await logout();
      // if(res.code==200){
        context.commit('deleteAllTagView')
        context.commit('setUserInfo',null)
        context.commit('setToken',null)
        router.push("login")
      // } 
      return res
    },
    // token失效处理
    async exit1(context){
      
      
        context.commit('deleteAllTagView')
        context.commit('setUserInfo',null)
        context.commit('setToken',null)
        router.push("login")
      
    },
    setState(context,storage){
      context.commit('setState', storage)
    },
    setBreadCrumb(context, breadcrumb) {
      context.commit('setBreadCrumb', breadcrumb)
    },

  },
  modules: {
  }
})
